define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'teacher_order/index' + location.search,
                    edit_url: 'teacher_order/edit',
                    multi_url: 'teacher_order/multi',
                    import_url: 'teacher_order/import',
                    del_url: 'teacher_order/del',
                    table: 'teacher_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                showToggle: false,
                exportTypes : ['excel'],
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname',operate: 'LIKE', title: __('User_id'), width: '200',formatter:function(v,row){
                            if(row.user){
                                return '<div style="display:flex"><img width="60px" height="60px" src="'+Fast.api.cdnurl(row.user.avatar)+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.user.id+'<br/>'+row.user.nickname+'<br/>'+row.user.mobile+'</p></div>';
                            }else{
                                return '';
                            }

                            }
                        },
                        {field: 'teacher.nickname',operate: 'LIKE', title: __('Teacher_id'), width: '200',formatter:function(v,row){
                            if(row.teacher && row.teacher.avatar){
                                return '<div style="display:flex"><img width="60px" height="60px" src="'+Fast.api.cdnurl(row.teacher.avatar)+'" /><p style="text-align:left;line-height:20px;margin-top:5px;margin-left:10px">ID:'+row.teacher.user_id+'<br/>'+row.teacher.nickname+'<br/>'+row.teacher.mobile+'</p></div>';
                            }else{
                                return '教师已被删除';
                            }}
                        },
                        {field: 'subject_name', title: __('Subject_id')},
                        {field: 'grade_name', title: __('Grade_id')},
                        {field: 'first_teacher', title: __('Pirst_teacher'), searchable: false},
                        {field: 'per_week_time_text', title: __('Per_week_time'), searchable: false},
                        {field: 'abnormal', title: '是否交易异常',formatter:function(v,row){
                            if(row.abnormal==''){
                                return '正常';
                            }else{
                                return '<span style="color:red;">异常</span>';
                            }
                        }},
                        {field: 'status', title: __('Status'), searchList: {"-3":__('Status -3'),"-2":__('Status -2'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_deposit', title: __('教师接单押金'), searchList: {"0":__('未缴纳'),"1":__('已缴纳'),"2":__('已退还')}, formatter: Table.api.formatter.status},
                        {field: 'pay_agency', title: __('教师接单中介费'), searchList: {"0":__('未缴纳'),"1":__('已缴纳'),"2":__('已退还')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons :[
                                {
                                    name: 'deposit',
                                    text: __('退教师接单押金'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    visible: function (row){
                                      if(row.pay_deposit==1){
                                          return true;
                                      }else{
                                          return false;
                                      }
                                    },
                                    icon: 'fa fa-rotate-left',
                                    url: 'teacher_order/refund_deposit',
                                    refresh: true
                                },
                                {
                                    name: 'agency',
                                    text: __('退教师接单中介费'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    visible: function (row){
                                        if(row.pay_agency==1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    icon: 'fa fa-rotate-left',
                                    url: 'teacher_order/refund_agency',
                                    refresh: true
                                },
                                {
                                    name: 'abnormal',
                                    text: __('已处理'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    visible: function (row){
                                        if(row.abnormal!==''){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    icon: 'fa fa-rotate-left',
                                    url: 'teacher_order/handle_abnormal',
                                    refresh: true
                                }
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
